﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    class AB50Report : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        public DataSet setUI = new DataSet();
        string ABPAUFFKRS;
        string ABPAUFFLFD;
        //string ABPAUFFPOS;
        string DocType;
        string CellName = "";
        string CellValue = "";
        string strCompany;
        string strExcelFile;
        #endregion
        public AB50Report()
            : base()
        {
        }
        public AB50Report(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            ABPAUFFKRS = dicParamater["ABPAUFKRS"].ToString().Trim();
            ABPAUFFLFD = dicParamater["ABPAUFLFD"].ToString().Trim();
            //ABPAUFFPOS = dicParamater["ABPAUFPOS"].ToString().Trim();
            DocType = dicParamater["DocType"].ToString().Trim();
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
        }
        #region------------------------------Function------------------------
        private void FillInOrderHead(DataTable dtSource)
        {
            try
            {
                Queue CellQueue = new Queue();
                CellQueue.Clear();

                foreach (DataRow dwSource in dtSource.Rows)
                {

                    CellName = "STAFF";
                    CellValue = dwSource["HRNNAME"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "TEL";
                    CellValue = dwSource["HRDTELH"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "FAX";
                    CellValue = dwSource["HRDFAX"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                   /* CellName = "ORDERDATE";
                    CellValue = dwSource["ABKAEDAT"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);*/
                    CellName = "REMARK";
                    CellValue = dwSource["COMMENTS"].ToString().Replace("\n", "").Replace(" ", "");
                    exAgent.SetCellValue(CellName, CellValue, true);
                    /*CellQueue.Enqueue("STAFF");
                    CellQueue.Enqueue("HRNNAME");
                    CellQueue.Enqueue("TEL");
                    CellQueue.Enqueue("HRDTELH");
                    CellQueue.Enqueue("FAX");
                    CellQueue.Enqueue("HRDFAX");
                    CellQueue.Enqueue("ORDERDATE");
                    CellQueue.Enqueue("ABKAEDAT");*/

                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
        } 
        private void FillInOrderPos(DataTable dtSource)
        {
            try
            {

                Queue CellQueue = new Queue();
                CellQueue.Clear();

                string ORDER = "ORDER";
                string YARN = "YARN";
                string QUANTITY = "QUANTITY";
                string BATCH = "BATCH";
                string DESCRIPTION = "DESCRIPTION";
                string COMPOSITION = "COMPOSITION";
                string COLOR = "COLOR";
                string FACTORY = "FACTORY";
                string GREIGE = "GREIGE";
                string USED = "USED";
                string COMMENTS = "COMMENTS";
                string PRICE = "PRICE";
                string SPPART = "SPPART";

                int I = 0;
                string J = "00";

                foreach (DataRow dwSource in dtSource.Rows)
                {
                    I++;
                    J = Right("00" + I.ToString(), 2);
                    CellName = ORDER + J;
                    CellValue = dwSource["ABPAUFKRS"].ToString() + "-" + dwSource["FLFD"].ToString() + "-" + dwSource["FPOS"].ToString();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = YARN + J;
                    CellValue = dwSource["ABPART"].ToString().Trim() + "/" + dwSource["ABPFARBE"].ToString().Trim() + "/" + dwSource["ABPVAR"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = QUANTITY + J;
                    CellValue = dwSource["WEI"].ToString();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = BATCH + J;
                    CellValue = dwSource["ABPLTVORL"].ToString();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = DESCRIPTION + J;
                    CellValue = dwSource["GSGARNBEZ"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = COMPOSITION + J;
                    CellValue = dwSource["COMPONENT"].ToString();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = COLOR + J;
                    CellValue = dwSource["GSCBEZ"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = FACTORY + J;
                    CellValue = dwSource["FACTORY"].ToString();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = GREIGE + J;
                    CellValue = dwSource["FABRIC"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = USED + J;
                    CellValue = dwSource["USAGE"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = COMMENTS + J;
                    CellValue = dwSource["REMARK"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = PRICE + J;
                    CellValue = dwSource["PRICE"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = SPPART + J;
                    CellValue = dwSource["SPPART"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "ABPNEUDAT" + J;
                    CellValue = dwSource["ABPNEUDAT"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "ABPSTARTDAT" + J;
                    CellValue = dwSource["ABPSTARTDAT"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "ABPENDDAT" + J;
                    CellValue = dwSource["ABPENDDAT"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);

                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }

        }
    

        public void GetAB50Report(string fileType)
        {

            // var query = from dwABPOS in dsSource.Tables["ABPOS"].AsEnumerable()
            //      select dwABPOS;

            //string strSheetName;
            // string TableName = "";
            // int Rown = 0;
            // string sRow = "";
            Queue CellQueue = new Queue();
            string value = "";
            string filename = "";
            string sheetName = "";
            string Standard = "Yarn";
            switch (fileType)
            {
                case "EXCEL":

                    string filePath = @"C:\intex.w\Report\" + strExcelFile;
                    filePath = strExcelFile;
                    exAgent.Open(filePath);
                    exAgent.Company = strCompany;
                    exAgent.strExcelFile = strExcelFile;
                    //foreach (DataRow dwWASTAMM in dsSource.Tables["WASTAMM"].Rows)
                    if (true)
                    {
                        //exAgent = CreateNewSheet(exAgent, dwWASTAMM);
                        filename = @"C:\intex.w\Report\" + ABPAUFFKRS + ABPAUFFLFD + ".xls";
                        sheetName = ABPAUFFKRS + "_" + ABPAUFFLFD;
                        exAgent.CreateNewSheet(filename, Standard, sheetName);
                        CellName = "PRINTDATE";
                        value = System.DateTime.Today.Year.ToString()+System.DateTime.Today.Month.ToString()+System.DateTime.Today.Day.ToString();
                        exAgent.SetCellValue(CellName, value, true);
                        /*CellName = "SENDINGDATE";
                        value = System.DateTime.Today.ToShortDateString();
                        exAgent.SetCellValue(CellName, value, true);*/
                        //CellName = "DELDATE";
                        //value = System.DateTime.Today.ToShortDateString();
                        //exAgent.SetCellValue(CellName, value, true);
                        CellName = "PageNum";
                        value = "1";
                        exAgent.SetCellValue(CellName, value, true);
                        FillInOrderHead(dsSource.Tables["ABKOPF"]);
                        FillInOrderPos(dsSource.Tables["ABPOS"]);
                        //FillInABFOLGE(dsSource.Tables["ABFOLGE"]);
                        //FillInABAUFM(dsSource.Tables["ABAUFM"]);
                        //FillInGSDAT(dsSource.Tables["GSDAT"]);
                    }

                    break;
                default:
                    break;
            }
            if (exAgent != null)
            {
                if (strCompany == "HLT")
                {
                    //HideNullRow();
                }
                else
                {
                    //HideNullRow();
                }
                exAgent.Save();
                //MessageBox.Show("你是否打开Exclel?",,MessageBoxButtons.YesNo,MessageBoxDefaultButton.Button2);

                exAgent.Close();
            }
        }

        #endregion
    }
}